package net.siufung.mybatis.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.core.page.IPager;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.ResultFail;
import net.siufung.core.utils.ValidationUtil;
import net.siufung.core.utils.ValidationUtil.ValidResult;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/2/9 11:37 下午
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    @Override
    public T getById(Serializable id) {
        if(Objects.isNull(id)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ID_ERROR);
        }
        return super.getById(id);
    }

    /**
     * 通过条件获取分页
     * @param pageNo 当前页
     * @param pageSize 页大小
     * @param queryWrapper 查询条件
     * @return IPager<T>
     */
    public IPager<T> pager(int pageNo, int pageSize, Wrapper<T> queryWrapper){
        Page<T> page = page(new Page<>(pageNo,pageSize), queryWrapper);
        return new PagerResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 通过条件获取分页
     * @param pageNo 当前页
     * @param pageSize 页大小
     * @param queryWrapper 查询条件
     * @return IPager<Map<String, Object>>
     */
    public IPager<Map<String, Object>> pagerMaps(int pageNo, int pageSize, Wrapper<T> queryWrapper){
        Page<Map<String, Object>> page = pageMaps(new Page<>(pageNo,pageSize), queryWrapper);
        return new PagerResult<>(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    @Override
    public boolean save(T entity) {
        ValidResult validResult = ValidationUtil.validateBean(entity);
        if(validResult.hasErrors()){
            throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
        }
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList){
        if(CollectionUtils.isEmpty(entityList)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ERROR);
        }
        ValidResult validResult;
        for (T entity: entityList) {
            validResult = ValidationUtil.validateBean(entity);
            if(validResult.hasErrors()){
                throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
            }
        }
        return super.saveBatch(entityList);
    }

    @Override
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        if(CollectionUtils.isEmpty(entityList)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ERROR);
        }
        ValidResult validResult;
        for (T entity: entityList) {
            validResult = ValidationUtil.validateBean(entity);
            if(validResult.hasErrors()){
                throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
            }
        }
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    public boolean updateById(T entity) {
        ValidResult validResult = ValidationUtil.validateBean(entity);
        if(validResult.hasErrors()){
            throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
        }
        return super.updateById(entity);
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList) {
        if(CollectionUtils.isEmpty(entityList)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ERROR);
        }
        ValidResult validResult;
        for (T entity: entityList) {
            validResult = ValidationUtil.validateBean(entity);
            if(validResult.hasErrors()){
                throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
            }
        }
        return super.updateBatchById(entityList);
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        if(CollectionUtils.isEmpty(entityList)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ERROR);
        }
        ValidResult validResult;
        for (T entity: entityList) {
            validResult = ValidationUtil.validateBean(entity);
            if(validResult.hasErrors()){
                throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
            }
        }
        return super.updateBatchById(entityList, batchSize);
    }

    @Deprecated
    @Override
    public boolean saveOrUpdate(T entity) {
        throw new BizServiceException("请分别使用save或者updateById实现功能！");
    }

    @Deprecated
    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList) {
        throw new BizServiceException("请分别使用SaveBatch或者UpdateBatchById实现功能！");
    }

    @Deprecated
    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        throw new BizServiceException("请分别使用SaveBatch或者UpdateBatchById实现功能！");
    }

    @Override
    public boolean removeById(Serializable id) {
        if(Objects.isNull(id)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ID_ERROR);
        }
        return super.removeById(id);
    }

    @Deprecated
    @Override
    public boolean removeById(T entity) {
        throw new BizServiceException("请使用removeById(Serializable id)");
    }

    @Deprecated
    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        throw new BizServiceException("请使用removeById(Serializable id)");
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if(CollectionUtils.isEmpty(idList)){
            throw new BizServiceException(ResultFail.PARAM_MISS_ERROR);
        }
        return super.removeByIds(idList);
    }

    @Override
    public boolean remove(T entity) {
        ValidResult validResult = ValidationUtil.validateBean(entity);
        if(validResult.hasErrors()){
            throw new BizServiceException(ResultFail.PARAM_VALID_ERROR+":【"+validResult.getErrors()+"】");
        }
        return remove(Wrappers.query(entity));
    }

    @Override
    public boolean  updateStatusById(Serializable id, DeletedEnum deletedEnum) {
        return false;
    }

}
